package cn.vconinfo.app.response;

import cn.vconinfo.app.enums.ServiceReturnCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;

/**
 * @author vconinfo
 * @createdOn 2019/07/22
 * @description: 接口返回工具
 */
public class ControllerReturn {
	public ControllerReturn() {
	}

	@SuppressWarnings({ "serial" })
	public static ResponseEntity<?> returnNormal(final ServiceReturn<?> srt) {
		return !ServiceReturnCode.SUCCESS.equals(srt.getReturnCode())
				? (ServiceReturnCode.ERROR.equals(srt.getReturnCode())
						? new ResponseEntity<Object>(new HashMap<String, String>() {
							{
								this.put("message", srt.getMessage());
							}
						}, HttpStatus.EXPECTATION_FAILED) : new ResponseEntity<Object>(new HashMap<String, String>() {
							{
								this.put("message", srt.getMessage());
							}
						}, HttpStatus.INTERNAL_SERVER_ERROR))
				: new ResponseEntity<Object>(srt.getData(), HttpStatus.OK);
	}

	public static ResponseEntity<?> returnError(final Exception ex) {
		return new ResponseEntity<Object>(new HashMap<String, String>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			{
				this.put("message", ex.getMessage());
			}
		}, HttpStatus.INTERNAL_SERVER_ERROR);
	}
}
